﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StudentInfoSystem.View
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        public bool TryLogin(out string loggedInUserName)
        {
            loggedInUserName = String.Empty;
            this.ShowDialog();
            if (this.DialogResult == DialogResult.Cancel)
            {
                return false;
            }

            string userName = this.textBoxUserName.Text;
            string password = this.textBoxPassword.Text;
            if (this.IsLoginSuccessful(userName,password))
            {
                loggedInUserName = userName;
                return true;
            }
            return false;
        }

        protected bool IsLoginSuccessful(string userName, string password)
        {
            bool isSuccessfull = true;
            return isSuccessfull;
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.Cancel)
            {
                return;
            }
            bool isFormValid = true;
            this.LabelError.Text = String.Empty;
            if (!Logic.Validation.ValidateUsername(this.textBoxUserName.Text))
            {
                isFormValid = false;
                this.LabelError.Visible = true;
                this.LabelError.Text = "Invalid Username";
            }
            if (!Logic.Validation.ValidatePassword(this.textBoxPassword.Text))
            {
                isFormValid = false;
                this.LabelError.Visible = true;
                this.LabelError.Text += "\nInvalid Password";
            }
            else if (!Logic.Validation.ValidateGivenUser(this.textBoxUserName.Text, this.textBoxPassword.Text))
            {
                isFormValid = false;
                this.LabelError.Visible = true;
                this.LabelError.Text = "Wrong login information";
            }
            if (!isFormValid)
            {
                e.Cancel = true;
            }
        }

        private void buttonОК_Click(object sender, EventArgs e)
        {
        }
    }
}
